<template>
  <div class="pagination">
    <button :disabled="pageNo === 1" @click="changePageNow(pageNo - 1)">
      上一页
    </button>
    <button v-show="startEnd.start !== 1" @click="changePageNow(1)">1</button>
    <span v-show="startEnd.start > 2">···</span>

    <!-- 起始页：当前页码 - (连续页数 - 1) / 2
         结束页：当前页码 + (连续页数 - 1) / 2 -->
    <button
      v-for="(item, index) in startEnd.end - startEnd.start + 1"
      :key="index"
      :class="{ active: startEnd.start + index === pageNo }"
      @click="changePageNow(startEnd.start + index)"
    >
      {{ startEnd.start + index }}
    </button>
    <!-- 当总页数-1 大于 页面展示的最后一页时 需要显示... -->
    <span v-show="totalPage - 1 > startEnd.end">···</span>
    <button
      v-show="startEnd.end !== totalPage"
      @click="changePageNow(totalPage)"
    >
      {{ totalPage }}
    </button>
    <button :disabled="pageNo === totalPage" @click="changePageNow(pageNo + 1)">
      下一页
    </button>

    <span>共 {{ total }} 条</span>
  </div>
</template>

<script>
export default {
  name: 'VPagination',
  props: {
    // 总数据、总条数
    total: {
      type: Number,
      default: 0
    },
    // 每页展示多少条
    pageSize: {
      type: Number,
      default: 5
    },
    // 当前页
    pageNo: {
      type: Number,
      default: 1
    },
    // 展示的分页数
    continues: {
      type: Number,
      default: 5
    }
  },
  computed: {
    // 总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize)
      // return console.log(this.pageSize)
    },
    // 计算页面展示头尾页数
    startEnd() {
      let start = 0
      let end = 0
      // 特殊情况：用户传入的 continues > totalPage
      if (this.continues > this.totalPage) {
        start = 1
        end = this.totalPage
      } else {
        start = this.pageNo - (this.continues - 1) / 2
        end = this.pageNo + (this.continues - 1) / 2
        // 计算出来的start < 1
        if (start < 1) {
          start = 1
          end = this.continues
        }
        // 计算出来的 end > totalPage
        if (end > this.totalPage) {
          start = this.totalPage - this.continues + 1
          end = this.totalPage
        }
      }
      return { start, end }
    }
  },
  methods: {
    changePageNow(newPageNo) {
      // console.log(newPageNo)
      // 需要将最新的页码传递给父组件，因为页面是父组件传递过来的
      this.$emit('sendNewPageNo', newPageNo)
    }
  }
}
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;

  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: gray;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #c81623;
      color: #fff;
    }
  }

  span {
    display: inline-block;
    line-height: 28px;
    font-size: 14px;
    color: gray;
    vertical-align: middle;
  }
}
</style>
